<?php
class Penggunaan_model extends CI_Model {

    var $tabel    = 'pengiriman';
	var $tabel_d  = 'pengirimandetail';

    function __construct() {
        parent::__construct();
		$this->load->library('encrypt');
    }
    
    function get_count($options=array())
    {
		$this->db->like($options);
        $query = $this->db->get($this->tabel);
		return count($query->result_array());
    }

    function get_data()
    {
		//$this->db->like($options);
        $query = $this->db->get($this->tabel);
        return $query->result();
    }
	
	function get_data_join(){
		$sql = "SELECT a.*,b.nmjenis,IFNULL(c.jumlah,0) as jumlah
				FROM barang a
					INNER JOIN jenis b
						ON a.kdjenis=b.kdjenis
					LEFT JOIN (
						SELECT IFNULL(COUNT(idbarang),0) AS jumlah,idbarang
						FROM barangdetail
						GROUP BY idbarang
					) c
						ON a.idbarang=c.idbarang
					";
		$query = $this->db->query($sql);
		return $query->result();
	}

 	function get_data_row($kd){
		$data = array();
		$options = array('nosuratjalan' => $kd);
		$query = $this->db->get_where($this->tabel,$options,1);
		if ($query->num_rows() > 0){
			$data = $query->row_array();
		}

		$query->free_result();    
		return $data;
	}
	
	function get_data_detail($kd){
		$sql = "SELECT a.*,c.nmbarang,c.berat 
					FROM pengirimandetail a 
						INNER JOIN barangdetail b 
							ON a.idbarangdetail=b.idbarangdetail
						INNER JOIN barang c
							ON b.idbarang=c.idbarang
				WHERE a.nosuratjalan='$kd'";
		$query = $this->db->query($sql);
		return $query->result_array();
	}

   function insert_entry()
    {
		$data['nosuratjalan']=$this->input->post('nosuratjalan');
		$data['tglsurat']=$this->input->post('tglsurat');
		$data['tglkirim']=$this->input->post('tglkirim');
		$data['lokasi']=$this->input->post('lokasi');
		$data['daerah']=$this->input->post('daerah');
		$data['pembuat']=$this->input->post('pembuat');
		$data['pemeriksa']=$this->input->post('pemeriksa');
		$data['peminta']=$this->input->post('peminta');
		$data['tujuan']=$this->input->post('tujuan');
		$data['rig']=$this->input->post('rig');
		$data['namavendor']=$this->input->post('namavendor');
		$data['alamat']=$this->input->post('alamat');
		$data['negara']=$this->input->post('negara');
		$data['transportasi']=$this->input->post('transportasi');
		
		$this->db->insert($this->tabel, $data);
		return $this->input->post('nosuratjalan');
    }

    function insert_entry_detail($kd)
    {
		for($x=1;$x<=intval($this->input->post('jmlDetail'));$x++){
			if($this->input->post('idbarangdetail'.$x)!="" && $this->input->post('kdstatus'.$x)!=4){
				$data['nosuratjalan']=$kd;
				$data['idbarangdetail']=$this->input->post('idbarangdetail'.$x);
				$data['kondisi']=$this->input->post('kondisi'.$x);
				$data['keterangan']=$this->input->post('keterangan'.$x);
				$ret = $this->db->insert($this->tabel_d, $data);
				$this->db->update("barangdetail", array("kdstatus"=>4,"keterangan"=>$this->input->post('keterangan'.$x)), array('idbarangdetail' => $this->input->post('idbarangdetail'.$x)));
			}
		}
		return $ret;
    }
	

	function delete_entry($kd)
	{
		$this->db->where(array('idbarang' => $kd));

		return $this->db->delete($this->tabel);
	}
	
	function get_nomor(){
		$sql = "SELECT IFNULL(SUBSTRING(MAX(nosuratjalan),7,4),0) AS maks FROM pengiriman
				WHERE SUBSTRING(nosuratjalan,1,6)='SJ".date('ym')."'";
		$query = $this->db->query($sql);
		$data = $query->row_array();
		$sequen = intval($data["maks"])+1;
		if($sequen<10)
			$sj = date('ym')."000".$sequen;
		else if($sequen<100)
			$sj = date('ym')."00".$sequen;
		else if($sequen<1000)
			$sj = date('ym')."0".$sequen;
		else if($sequen<10000)
			$sj = date('ym').$sequen;
		else
			$sj = date('ym')."err";
			
		return "SJ".$sj;
	}

}